home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / expect / pty_unicos.c < prev    next >
C/C++ Source or Header  |  1993-03-15  |  9KB  |  365 lines

  1. /* pty_unicos.c - routines to allocate ptys - for CRAY UNICOS 5.1 and 6.0 */
  2.  
  3. /*
  4.  
  5. Original by: Don Libes, NIST, 2/6/90
  6. Hacked for Unicos 5.1 by: Frank Terhaar-Yonkers, US EPA,  1/10/91
  7. Hacked for Unicos 6.0 by: Pete TerMaat, pete@willow.cray.com, 3/27/91
  8.  
  9. Design and implementation of this program was paid for by U.S. tax
  10. dollars.  Therefore it is public domain.  However, the author and NIST
  11. would appreciate credit if this program or parts of it are used.
  12.  
  13. */
  14.  
  15. #include "exp_conf.h"
  16. #include <stdio.h>
  17. #ifdef HAVE_UNISTD_H
  18. #include <unistd.h>
  19. #else
  20. extern int fork(), execl(), wait();
  21. #endif
  22. #include <errno.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/file.h>
  27. #ifdef HAVE_SYS_FCNTL_H
  28. #  include <sys/fcntl.h>
  29. #else
  30. #  include <fcntl.h>
  31. #endif
  32. #if CRAY>=60
  33. #include <sys/termios.h>
  34. #else
  35. #include <sys/termio.h>
  36. #endif /* 60 */
  37. #if CRAY>=70 && defined(_CRAY2)
  38. #include <sys/session.h>
  39. #endif /* 70 */
  40. #include <sys/pty.h>
  41. #include <pwd.h>
  42. #include <utmp.h>
  43. #include <signal.h>
  44. #include "exp_rename.h"
  45.  
  46. #ifdef HAVE_SYSCONF_H
  47. #include <sys/sysconfig.h>
  48. #endif
  49.  
  50. void debuglog();
  51.  
  52. #if 0
  53. /* changed this to depend on HAVE_UNISTD_H earlier - DEL */
  54. #if CRAY<60
  55. extern int fork(), execl(), wait();
  56. #endif
  57. #endif
  58.  
  59. #ifndef TRUE
  60. #define TRUE 1
  61. #define FALSE 0
  62. #endif
  63.  
  64. #ifndef MAXHOSTNAMELEN
  65. #define MAXHOSTNAMELEN 64
  66. #endif /* MAXHOSTNAMELEN */
  67.  
  68. static char    linep[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  69. static char    linet[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  70. static int    lowpty;
  71. static int    highpty;
  72. static int    realuid;
  73. static int    realgid;
  74. static int     *ptys;
  75. static char myname[32];
  76. static char hostname[MAXHOSTNAMELEN];
  77.  
  78. static void
  79. pty_stty(s,name)
  80. char *s;        /* args to stty */
  81. char *name;        /* name of pty */
  82. {
  83. #define MAX_ARGLIST 10240
  84.     char buf[MAX_ARGLIST];    /* overkill is easier */
  85.  
  86.     sprintf(buf,"stty %s < %s > %s",s,name,name);
  87.     system(buf);
  88. }
  89.  
  90. struct    termio exp_tty_original;
  91.  
  92. int dev_tty;        /* file descriptor to /dev/tty or -1 if none */
  93. int knew_dev_tty;    /* true if we had our hands on /dev/tty at any time */
  94.  
  95. #define GET_TTYTYPE    0
  96. #define SET_TTYTYPE    1
  97. static void
  98. ttytype(request,fd,ttycopy,ttyinit,s)
  99. int request;
  100. int fd;
  101.         /* following are used only if request == SET_TTYTYPE */
  102. int ttycopy;    /* true/false, copy from /dev/tty */
  103. int ttyinit;    /* if true, initialize to sane state */
  104. char *s;    /* stty args, used only if request == SET_TTYTYPE */
  105. {
  106.     if (request == GET_TTYTYPE) {
  107.         if (-1 == ioctl(fd, TCGETA, (char *)&exp_tty_original)) {
  108.             knew_dev_tty = FALSE;
  109.             dev_tty = -1;
  110.         }
  111.     } else {    /* type == SET_TTYTYPE */
  112.         if (ttycopy && knew_dev_tty) {
  113.             (void) ioctl(fd, TCSETA, (char *)&exp_tty_original);
  114.         }
  115.  
  116.         if (ttyinit) {
  117.             /* overlay parms originally supplied by Makefile */
  118.             debuglog("ttytype: (default) stty %s\n",DFLT_STTY);
  119.             pty_stty(DFLT_STTY,linet);
  120.         }
  121.  
  122.         /* lastly, give user chance to override any terminal parms */
  123.         if (s) {
  124.             debuglog("ttytype: (user-requested) stty %s\n",s);
  125.             pty_stty(s,linet);
  126.         }
  127.     }
  128. }
  129.  
  130. void
  131. init_pty()
  132. {
  133.     int npty;
  134.     char *myline;
  135.  
  136.     lowpty=0;
  137. #ifdef _SC_CRAY_NPTY
  138.     highpty=sysconf(_SC_CRAY_NPTY);
  139. #else
  140.     highpty=128;
  141. #endif /* _SC_CRAY_NPTY */
  142.  
  143.     ptys = (int *) malloc(sizeof(int)*(highpty+1));
  144.     if (ptys == NULL) {
  145.         fprintf(stderr,"init_pty:  couldn't allocate pty array\n");
  146.         exit(1);
  147.     }
  148.     for (npty = lowpty;npty <= highpty;npty++)
  149.         ptys[npty] = 0;
  150.  
  151.      realuid=getuid();    /* get REAL uid */
  152.      realgid=getgid();    /* get REAL uid */
  153.  
  154.     dev_tty = open("/dev/tty",O_RDWR);
  155.     knew_dev_tty = (dev_tty != -1);
  156.     if (knew_dev_tty) ttytype(GET_TTYTYPE,dev_tty,0,0,(char *)0);
  157.  
  158.     /*
  159.      * Acquire (as root) current user name and host.
  160.      */
  161.     (void) cuserid(myname);
  162.     (void) gethostname(hostname,sizeof(hostname));
  163.  
  164.     /*
  165.      * Set the real and effective userids to root using 'setuid'.  Then
  166.      * set the real and effective userids to the actual user using
  167.      * 'setreuid'.  This allows using 'seteuid' to go back and forth from
  168.      * root and the actual userid.  Don't ask me why it works.
  169.      */
  170.     setuid(0);
  171.     setreuid(realuid,realuid);
  172. }
  173.  
  174. /* returns fd of master end of pseudotty */
  175. int
  176. getptymaster()
  177. {
  178.     struct stat sb;
  179.     int master;
  180.     int npty;
  181.  
  182.     debuglog("getptymaster:  lowpty=%d  highpty=%d\n",lowpty,highpty);
  183.     for (npty = lowpty; npty <= highpty; npty++) {
  184.         if (seteuid(0) == -1) {        /* we need to be root! */
  185.             debuglog("getptymaster:  seteuid root errno=%d\n",
  186.                 errno);
  187.         }
  188.         (void) sprintf(linep, "/dev/pty/%03d", npty);
  189.         master = open(linep, O_RDWR);
  190.  
  191.         if (master < 0) {
  192.             debuglog("getptymaster:  open linep=%s errno=%d\n",
  193.                 linep,errno);
  194.             continue;
  195.         }
  196.  
  197.         (void) sprintf(linet, "/dev/ttyp%03d", npty);
  198.         if(stat(linet, &sb) < 0) {
  199.             debuglog("getptymaster:  stat linet=%s errno=%d\n",
  200.                 linet,errno);
  201.             (void) close(master);
  202.             continue;
  203.         }
  204.         if (sb.st_uid || sb.st_gid || sb.st_mode != 0600) {
  205.                         if (chown(linet, realuid, realgid) == -1) {
  206.                 debuglog("getptymaster:  chown linet=%s errno=%d\n",
  207.                     linet,errno);
  208.             }
  209.                         if (chmod(linet, 0600) == -1) {
  210.                 debuglog("getptymaster:  chmod linet=%s errno=%d\n",
  211.                     linet,errno);
  212.             }
  213.                         (void)close(master);
  214.                         master = open(linep, 2);
  215.                         if (master < 0) {
  216.                 debuglog("getptymaster:  reopen linep=%s errno=%d\n",
  217.                     linep,errno);
  218.                                 continue;
  219.             }
  220.                 }
  221.         if (seteuid(realuid) == -1) {    /* back to who we are! */
  222.             debuglog("getptymaster:  seteuid user errno=%d\n",
  223.                 errno);
  224.         }
  225.         if (access(linet, R_OK|W_OK) != 0) {
  226.             debuglog("getptymaster:  access linet=%s errno=%d\n",
  227.                 linet,errno);
  228.             (void) close(master);
  229.             continue;
  230.         }
  231.         debuglog("getptymaster:  allocated %s\n",linet);
  232.         ptys[npty] = -1;
  233.         return(master);
  234.     }
  235.     if (seteuid(realuid) == -1) {        /* back to who we are! */
  236.         debuglog("getptymaster:  seteuid user errno=%d\n",errno);
  237.     }
  238.     return(-1);
  239. }
  240.  
  241. int
  242. getptyslave(ttycopy,ttyinit,stty_args)
  243. int ttycopy;
  244. int ttyinit;
  245. char *stty_args;
  246. {
  247.     int slave;
  248.  
  249.     if (0 > (slave = open(linet, O_RDWR))) {
  250.         debuglog("getptyslave:  open linet=%s errno=%d\n",linet,errno);
  251.         return(-1);
  252.     }
  253.  
  254.     /* sanity check - if slave not 0, skip rest of this and return */
  255.     /* to what will later be detected as an error in caller */
  256.     if (0 != slave) {
  257.         debuglog("getptyslave:  slave fd not 0\n");
  258.          return(slave);
  259.     }
  260.  
  261.     fcntl(0,F_DUPFD,1);    /* duplicate 0 onto 1 to prepare for stty */
  262.     ttytype(SET_TTYTYPE,slave,ttycopy,ttyinit,stty_args);
  263.     return(slave);
  264. }
  265.  
  266. setptyutmp()
  267. {
  268.     struct utmp utmp;
  269.  
  270.     if (seteuid(0) == -1) {        /* Need to be root */
  271.         debuglog("setptyutmp:  setuid root errno=%d\n",errno);
  272.         return(-1);
  273.     }
  274.     (void) time(&utmp.ut_time);
  275.     utmp.ut_type = USER_PROCESS;
  276.     utmp.ut_pid = getpid();
  277.     strncpy(utmp.ut_user,myname,sizeof(utmp.ut_user));
  278.     strncpy(utmp.ut_host,hostname,sizeof(utmp.ut_host));
  279.     strncpy(utmp.ut_line,linet+5,sizeof(utmp.ut_line));
  280.     strncpy(utmp.ut_id,linet+8,sizeof(utmp.ut_id));
  281.     if (pututline(&utmp) == NULL) {
  282.         debuglog("setptyutmp:  pututline failed\n");
  283.     }
  284.     endutent();
  285.     if (seteuid(realuid) == -1)
  286.         debuglog("setptyutmp:  seteuid user errno=%d\n",errno);
  287.     return(0);
  288. }
  289.  
  290. setptypid(pid)
  291. int pid;
  292. {
  293.         int npty;
  294.  
  295.         for (npty = lowpty; npty <= highpty; npty++) {
  296.                 if (ptys[npty] < 0) {
  297.                         debuglog("setptypid:  ttyp%03d pid=%d\n",npty,pid);
  298.                         ptys[npty] = pid;
  299.                         break;
  300.                 }
  301.         }
  302. }
  303.  
  304. ttyp_reset()
  305. {
  306.         int npty;
  307.  
  308.         if (seteuid(0) == -1) {        /* we need to be root! */
  309.                 debuglog("ttyp_reset:  seteuid root errno=%d\n",errno);
  310.         }
  311.         for (npty = lowpty; npty <= highpty; npty++) {
  312.                 if (ptys[npty] <= 0)
  313.                         continue;
  314.  
  315.                 (void) sprintf(linet, "/dev/ttyp%03d", npty);
  316.                 debuglog("ttyp_reset:  resetting %s, killing %d\n",
  317.             linet,ptys[npty]);
  318.                 if (chown(linet,0,0) == -1) {
  319.                         debuglog("ttyp_reset: chown %s errno=%d\n",linet,errno);
  320.                 }
  321.                 if (chmod(linet, 0666) == -1) {
  322.                         debuglog("ttyp_reset: chmod %s errno=%d\n",linet,errno);
  323.                 }
  324.                 resetptyutmp();
  325.                 if (kill(ptys[npty],SIGKILL) == -1) {
  326.                         debuglog("ttyp_reset:  kill pid=%d errno=%d\n",
  327.                                 ptys[npty],errno);
  328.                 }
  329.         }
  330.         if (seteuid(realuid) == -1) {   /* Back to who we really are */
  331.                 debuglog("ttyp_reset:  seteuid user errno=%d\n",errno);
  332.         }
  333. }
  334.  
  335. resetptyutmp()
  336. {
  337.         struct utmp utmp;
  338.  
  339.         (void) setutent ();
  340.         /* set up entry to search for */
  341.         (void) strncpy(utmp.ut_id, linet + strlen(linet) - 4,
  342.                  sizeof (utmp.ut_id));
  343.         utmp.ut_type = USER_PROCESS;
  344.  
  345.         /* position to entry in utmp file */
  346.         if(getutid(&utmp) == NULL) {
  347.                 debuglog("resetptyutmp:  no utmp entry for %s\n",linet);
  348.                 return(-1);     /* no utmp entry for this line ??? */
  349.         }
  350.  
  351.         /* set up the new entry */
  352.         strncpy(utmp.ut_name,"",sizeof(utmp.ut_name));
  353.         strncpy(utmp.ut_host,"",sizeof(utmp.ut_host));
  354.         time(&utmp.ut_time);
  355.         utmp.ut_type = DEAD_PROCESS;
  356.         utmp.ut_exit.e_exit = 0;
  357.  
  358.         /* write out the entry */
  359.         pututline(&utmp);
  360.  
  361.         /* close the file */
  362.         (void) endutent();
  363.         return(0);
  364. }
  365.